﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GoodUI : MonoBehaviour
{
	public KnapsackGood good { get; private set; }//物品槽中存放的物品
	public int amount { get; private set; }//物品槽存放物品的数量

	private Image GoodUIImage
	{
		get
		{
			return this.transform.GetComponent<Image>();
		}
	}

	private Text GoodUIText
	{
		get
		{
			return this.transform.GetComponentInChildren<Text>();
		}
	}

	public void SetGood(KnapsackGood good, int amount = 1)
	{
		this.good = good;
		this.amount = amount;


		GoodUIImage.sprite = Resources.Load<Sprite>(good.goodProperty.sprite);

		if (good.goodProperty.capacity > 1)
		{
			GoodUIText.text = this.amount.ToString();
		}
		else
		{
			GoodUIText.text = "";
		}
	}

	public void AddGood(int amount = 1)
	{
		this.amount += amount;

		if (good.goodProperty.capacity > 1)
		{
			GoodUIText.text = this.amount.ToString();

		}
		else
		{
			GoodUIText.text = "";
		}
	}

	public void ReplaceGoodUI(GoodUI gu)
	{
		this.good = gu.good;
		this.amount = gu.amount;

		Debug.Log(gu.amount);

		SetGood(this.good, this.amount);
	}

	public void Exchange(GoodUI gu)
	{
		KnapsackGood tempGood = gu.good;
		int tempAmount = gu.amount;


		gu.SetGood(good, amount);
		this.SetGood(tempGood, tempAmount);
	}


	public void SetLocalPosition(Vector3 point)
	{
		this.transform.localPosition = point;
	}

	public void Show()
	{
		this.gameObject.SetActive(true);
	}

	public void Hide()
	{
		this.gameObject.SetActive(false);
	}

}
